<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                mailRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#mailGird").jqGrid('getGridParam', 'selrow');
                        var mail = jQuery("#mailGird").jqGrid('getRowData', rowid);
                        dijit.byId('mailUFD').setValues(mail);
                    }
                };
                topic.subscribe("mail/insertOrUpdate", mailRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#mailGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#mailGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#mailGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#mailGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                mailSubmitForm = function(formId, type) {
                    var url = "mail/insert";
                    if (type == 'u') {
                        url = "mail/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues(), true);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("mail/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                mailDelete = function() {
                    var ids = jQuery("#mailGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var mail = {};
                            mail.id = '';
                            array.forEach(ids, function(entry, i) {
                                mail.id += entry + ",";
                            });
                            syncDB("mail/delete", JSON.stringify(mail, true));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                mailQuery = function(formId) {
                    var mail = dijit.byId(formId).getValues();
                    jQuery("#mailGird").jqGrid('setGridParam', {
                        postData : mail,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div id="mailIFD" >
    <div>
       <label for="recipientnames">收件人:</label>
       
       <input name="recipientnames" data-dojo-type="dijit/form/ValidationTextBox" type="text" name="recipientnames" autocomplete="off" size="20" style="width: 380px;">
       <!--  
       <div data-dojo-type="dojox/layout/ContentPane" data-dojo-props="href:'zTree'" title="TREE"></div>
        -->
    </div>
    <div>
    <label for="subject" style="width:60px;">主题:</label>
       <input data-dojo-type="dijit/form/ValidationTextBox" type="text" name="subject" autocomplete="off" size="20" style="width: 480px;">
    </div>
    <div>
       <label for="fileids">附件:</label>
       <input name="fileids" type="text" name="fileids" autocomplete="off" size="20" style="width: 480px;">
    </div>
    <div>
       <label style="width:60px;">内容:</label>
       <div data-dojo-type="dijit/Editor" name="content">
         <div style="width: 480px;"></div>
    </div>
    </div>
    
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="mailSubmitForm('mailIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="mailRestForm('mailIFD');">重置</button>
  </div>
  </div>